<!-- 首页 -->
<template>
  <div class="main-container">
    <a-spin :spinning="Data.spinning" tip="数据处理中，请耐心等待……">
      <list-row @changeFun="changeFun" :zpDataFlag="false">
        <template #title>
          <span style="font-weight: 700">科目总览</span>
        </template>
        <template #extra>
          <a-button
            class="mr-8px"
            :disabled="Data.kmpyjd != 'TEST' || !Data.pageAccountCodeKmdm"
            type="primary"
            v-if="route.meta?.buttonList?.includes('TrialBreakTest')"
            @click="releaseSetBreakTest"
          >
            终止试评
          </a-button>
          <a-button
            class="mr-8px"
            :disabled="
              (Data.kmpyjd != 'TEST' && Data.kmpyjd != 'OVER_TEST') || !Data.pageAccountCodeKmdm
            "
            type="primary"
            v-if="route.meta?.buttonList?.includes('TrialPublishFormal')"
            @click="openPositiveReview"
          >
            开启正评
          </a-button>
        </template>
      </list-row>
    </a-spin>
  </div>
</template>

<script lang="ts" setup>
import { h, reactive } from 'vue'

import { useRoute } from 'vue-router'
const route = useRoute()

import listRow from '/@/views/home/index/components/listRow.vue'

import { useMessage } from '/@/hooks/web/useMessage'
const { createConfirm, createMessage } = useMessage()
import { distributionPaper } from '/@/api/management/readAppraiseInfo'

import { setBreakTestPyProcess } from '/@/api/examinationAudit'
import { getAcountmdm } from '/@/api/examinationAudit'

const Data = reactive({
  pageAccountCodeKmdm: '', // 科目代码
  kmpyjd: '', // 科目状态
  spinning: false,
})

// 终止试评
const releaseSetBreakTest = async () => {
  createConfirm({
    iconType: 'warning',
    title: () => h('span', '温馨提醒'),
    content: () => h('span', `确认要终止科目${Data.pageAccountCodeKmdm}的试评吗？`),
    onOk: async () => {
      try {
        let { code, message } = await setBreakTestPyProcess({
          kmdm: Data.pageAccountCodeKmdm,
        })
        if (code == 200) {
          createMessage.info('试评已终止')
        } else {
          createMessage.error(message)
        }
      } catch (err) {
        console.log(err)
      }
    },
  })
}

// 开启正评
function openPositiveReview() {
  createConfirm({
    iconType: 'warning',
    title: () => h('span', '温馨提醒'),
    content: () =>
      h(
        'span',
        `确认要开启科目${Data.pageAccountCodeKmdm}的正评吗？开启正评后, 试评所有操作将关闭, 同时评阅标准将应用于正评评阅, 且无法再进行调整(可分配教师), 请确认是否开启正评`
      ),
    onOk: () => {
      openFormal()
    },
  })
}

function openFormal() {
  Data.spinning = true
  distributionPaper({
    kmdm: Data.pageAccountCodeKmdm,
  })
    .then(res => {
      if (res.code == 200) {
        createMessage.info('正评开启成功')
        changeFun(Data.pageAccountCodeKmdm)
      } else {
        createMessage.error(res.message)
      }
    })
    .finally(() => {
      Data.spinning = false
    })
}

async function changeFun(kmdm: any) {
  Data.kmpyjd = ''
  Data.pageAccountCodeKmdm = kmdm
  if (!kmdm || kmdm === '0') return
  const { code, data } = await getAcountmdm({ kmdm })
  if (code == 200) {
    Data.kmpyjd = data?.kmpyjd
  }
}
</script>

<style lang="less" scoped>
.main-container {
  display: flex;
  flex-direction: column;

  :global(.ant-pagination) {
    margin-top: 20px;
  }
}
</style>
